package phoenics.apeak.bus.config;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;

import com.lmax.disruptor.BlockingWaitStrategy;
import com.lmax.disruptor.EventFactory;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;

import phoenics.apeak.crawl.Document;
import phoenics.apeak.crawl.DocumentHandle;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
@ComponentScan(basePackages = {"phoenics.apeak.bus","phoenics.apeak.crawl"})
public class AppConfig implements ApplicationContextAware{
	private static ExecutorService  executor=Executors.newFixedThreadPool(16);  
	private  HttpClient httpClient_ =null;
	private ApplicationContext applicationContext;
	public static String redis_host="m2448"; //服务器名字 m2448
	public static int redis_port=6379;
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext = applicationContext;
	}
	@Bean
	public ExecutorService executorService()  throws Exception {
		return executor;  
	}
	
	@Bean
	   public HttpClient httpClient ()  throws Exception {
		if(this.httpClient_!=null){
			return this.httpClient_;
		}
		SslContextFactory sslContextFactory = new SslContextFactory();
		HttpClient httpClient_ =new HttpClient(sslContextFactory);
		httpClient_.setFollowRedirects(false);
		httpClient_.setExecutor(executorService());
		httpClient_.setIdleTimeout(10*1000);
		httpClient_.setRemoveIdleDestinations(true);
		//httpClient_.setAddressResolutionTimeout(7*1000);
		httpClient_.setMaxConnectionsPerDestination(50*5);
		httpClient_.start();
		this.httpClient_=httpClient_;
		return httpClient_;
	   }
	
	@SuppressWarnings("unchecked")
	@Bean
	   public  Disruptor<Document> disruptor() throws Exception {
			 int bufferSize=8*1024;  
			 ExecutorService  myExecutor=Executors.newFixedThreadPool(8);
		      Disruptor<Document> disruptor=new Disruptor<Document>(new EventFactory<Document>() {  
		            @Override  
		            public Document newInstance() {  
		                return new Document();  
		            }  
		        }, bufferSize,  myExecutor, ProducerType.SINGLE, new BlockingWaitStrategy());  
		      //disruptor.handleEventsWith(new DocumentHandle(httpClient ())).then(new FinishHandle());
		      //disruptor.handleEventsWith(new DocumentHandle(httpClient ()));
		      disruptor.handleEventsWith(new DocumentHandle(this.applicationContext));
		      disruptor.start();
		   return disruptor;
	   }
	
	private JedisPoolConfig jedisPoolConfig() throws Exception {
		JedisPoolConfig poolConfig=new JedisPoolConfig();
		poolConfig.setMaxTotal(600);
		poolConfig.setMaxIdle(300);
		poolConfig.setMaxWaitMillis(20000);
		poolConfig.setTestOnBorrow(false);
		poolConfig.setTestWhileIdle(false);
		return poolConfig;
	}
	@Bean
	public JedisConnectionFactory jedisConnectionFactory() throws Exception {
		JedisConnectionFactory connectionFactory=new JedisConnectionFactory();
		connectionFactory.setHostName(redis_host);
		connectionFactory.setPort(redis_port);
		connectionFactory.setPoolConfig(jedisPoolConfig());
		connectionFactory.setTimeout(10000);
		connectionFactory.setUsePool(true);
		return connectionFactory;
	}
	@Bean
	public StringRedisTemplate stringRedisTemplate() throws Exception {
		return new StringRedisTemplate( jedisConnectionFactory());
	}

	

}
